package cn.wolfcode.service.impl;

import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.mapper.MessageBoardMapper;
import cn.wolfcode.qo.MessageBoardQuery;
import cn.wolfcode.service.IMessageBoardService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessageBoardServiceImpl extends ServiceImpl<MessageBoardMapper, MessageBoard> implements IMessageBoardService {
    @Override
    public IPage<MessageBoard> queryPage(MessageBoardQuery qo) {
        Page<MessageBoard> page = new Page<>(qo.getCurrentPage(),qo.getPageSize());
        return super.page(page);
    }

    @Override
    public void saveMessage(MessageBoard messageBoard) {
        messageBoard.setCreatime(new Date());
        super.save(messageBoard);
    }

    @Override
    public List<String> searchAllAuthors() {
        QueryWrapper<MessageBoard> wrapper = new QueryWrapper<>();
        wrapper.select("author_name").groupBy("author_name");
        List<MessageBoard> list = list(wrapper);
        ArrayList<String> authors = new ArrayList<>();
        if (list.size() > 0){
            for (MessageBoard messageBoard : list) {
                authors.add(messageBoard.getAuthorName());
            }
        }
        return authors;
    }
}
